package top.mypath.service;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import top.mypath.model.Order;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author LiYalin
 */
@Slf4j
public class OrderExcelListener implements ReadListener<Order> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<Order> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    public List<Order> getCachedDataList() {
        return cachedDataList;
    }

    public void setCachedDataList(List<Order> cachedDataList) {
        this.cachedDataList = cachedDataList;
    }

    /**
     *
     * @param order
     * @param analysisContext
     */
    @Override
    public void invoke(Order order, AnalysisContext analysisContext) {
        if(order.getTotal().compareTo(new BigDecimal(0))==1) {
            cachedDataList.add(order);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("cachedDataList:"+JSON.toJSONString(cachedDataList));
    }
}
